import java.util.StringTokenizer;

class P101 {

    public static void main(String[] args) {
    	int numberOfBlocks = Integer.parseInt(readLn());
    	int[][] world = new int[numberOfBlocks][numberOfBlocks];
    	resetWorld(world);
    	String line = readLn();
    	while (!line.equals("quit")) {
    		process(line, world);
    		line = readLn();
    	}
    	printWorld(world);
    }

	private static void printWorld(int[][] world) {
		int length = world.length;
		for (int k = 0; k < length; k++) {
			System.out.print(k+":");
			for (int j = 0; j < length; j++) {
				if (world[k][j] != -1) {
					System.out.print(" "+world[k][j]);
				}
			}
			System.out.println();
		}
		
	}

	private static void process(String line, int[][] world) {
		StringTokenizer stringTokenizer = new StringTokenizer(line);
		String command = stringTokenizer.nextToken();
		int param1 = Integer.parseInt(stringTokenizer.nextToken());
		command += stringTokenizer.nextToken();
		int param2 = Integer.parseInt(stringTokenizer.nextToken());

		int[] param1_pos = whereIs(param1, world);
		int[] param2_pos = whereIs(param2, world);
		
		if (param1 != param2 && param1_pos[0] != param2_pos[0]) {
			if (command.equals("moveonto")) {
				moveonto(param1, param2, world);
			} else if (command.equals("moveover")) {
				moveover(param1, param2, world);
			} else if (command.equals("pileonto")) {
				pileonto(param1, param2, world);
			} else if (command.equals("pileover")) {
				pileover(param1, param2, world);
			}
		}
		
	}

	private static void pileover(int param1, int param2, int[][] world) {
		int[] param1_pos = whereIs(param1, world);
		int[] param2_pos = whereIs(param2, world);
		
		putAllInTheEnd(param1_pos, param2_pos, world);
	}

	private static int[] whereIs(int param1, int[][] world) {
		
		int length = world.length;
		for (int k = 0; k < length; k++) {
			for (int j = 0; j < length; j++) {
				if (param1 == world[k][j]) {
					int[] whereIs = new int[2];
					whereIs[0] = k;
					whereIs[1] = j;
					return whereIs;
				}
			}
		}
		return null;
	}

	private static void pileonto(int param1, int param2, int[][] world) {
		int[] param1_pos = whereIs(param1, world);
		int[] param2_pos = whereIs(param2, world);
		
		resetBlocks(param2_pos, world);
		
		putAllInTheEnd(param1_pos, param2_pos, world);
	}

	private static void putAllInTheEnd(int[] param1_pos, int[] param2_pos, int[][] world) {
		int length = world.length;
		for (int k = param1_pos[1]; k < length; k++) {
			int integer = world[param1_pos[0]][k];
			if (integer != -1) {
				int[] new_param_pos = { param1_pos[0], k };
				putInTheEnd(new_param_pos, param2_pos, world);
			} else {
				break;
			}
		}
	}

	private static void moveover(int param1, int param2, int[][] world) {
		int[] param1_pos = whereIs(param1, world);
		int[] param2_pos = whereIs(param2, world);
		
		resetBlocks(param1_pos, world);
		
		putInTheEnd(param1_pos, param2_pos, world);
	}

	private static void putInTheEnd(int[] param1_pos, int[] param2_pos, int[][] world) {
		int k = 0;
		for (k = param2_pos[1]; k < world[param2_pos[0]].length; k++){
			if (world[param2_pos[0]][k] == -1) {
				k--;
				break;
			}
		}
		int[] new_param2_pos = { param2_pos[0], k };
		put(param1_pos, new_param2_pos, world);
		
	}

	private static void moveonto(int param1, int param2, int[][] world) {
		int[] param1_pos = whereIs(param1, world);
		int[] param2_pos = whereIs(param2, world);
		
		resetBlocks(param1_pos, world);
		resetBlocks(param2_pos, world);
		
		put(param1_pos, param2_pos, world);
	}

	private static void put(int[] param1_pos, int[] param2_pos, int[][] world) {
		int param1 = world[param1_pos[0]][param1_pos[1]];
		world[param1_pos[0]][param1_pos[1]] = -1;
		world[param2_pos[0]][param2_pos[1]+1] = param1;
		
	}

	private static void resetBlocks(int[] param_pos, int[][] world) {
		int y = param_pos[0];
		int x = param_pos[1];
		
		int length = world.length;
		for (int j = x+1; j < length; j++) {
			int integer = world[y][j];
			world[y][j] = -1;
			if (integer != -1) {
				world[integer][0] = integer;
			}
		}
		
	}

	private static void resetWorld(int[][] world) {
		int length = world.length;
		for (int k = 0; k < length; k++) {
			world[k][0] = k;
			for (int j = 1; j < length; j++) {
				world[k][j] = -1;
			}
		}
		
	}

	static String readLn() {
        String newLine = System.getProperty("line.separator");
        StringBuffer buffer = new StringBuffer();
        int car = -1;
        try {
            car = System.in.read();
            while ((car > 0) && (car != newLine.charAt(0))) {
                buffer.append((char)car);
                car = System.in.read();
            }
            if (car == newLine.charAt(0))
            System.in.skip(newLine.length() - 1);
        } catch (java.io.IOException e) { return (null);}
        if ((car < 0) && (buffer.length() == 0)) return (null);
        return (buffer.toString()).trim();
    }

}